
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

/**
 * 98. 验证二叉搜索树
 * https://leetcode.cn/problems/validate-binary-search-tree/description/
 */
public class Solution {
    long prev = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if(root == null) return true;

        boolean left = isValidBST(root.left);
        if(left == false) return false;//剪枝
        boolean cur = false;
        if(root.val > prev) {
            prev = root.val;
            cur = true;
        } else {
            //剪枝
            return false;
        }
        boolean right = isValidBST(root.right);

        return left && right && cur;
    }
}